﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Net;

namespace client
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] data = new byte[1024];
            Socket newclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Console.Write("输入服务器地址:");
            string ipadd = Console.ReadLine();
            Console.WriteLine();
            Console.Write("输入服务端口");
            int port = Convert.ToInt32(Console.ReadLine());
            IPEndPoint ie = new IPEndPoint(IPAddress.Parse(ipadd),port);
            try
            {
                newclient.Connect(ie);
            }
            catch(SocketException e)
            {
                Console.WriteLine("不能连接");
                Console.WriteLine(e.ToString());
                return;
            }
            int recv=newclient.Receive(data);
            string stringdata=Encoding.ASCII.GetString(data,0,recv);
            Console.WriteLine(stringdata);
            while(true)
            {
                string input = Console.ReadLine();
                if (input == "exit")
                    break;
                newclient.Send(Encoding.ASCII.GetBytes(input));
                data=new byte[1024];
                recv = newclient.Receive(data);
                stringdata = Encoding.ASCII.GetString(data,0,recv);
                Console.WriteLine(stringdata);
            }
            Console.WriteLine("断开连接");
            newclient.Shutdown(SocketShutdown.Both);
            newclient.Close();
        }
    }
}
